//
//  YLAdView.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/27.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLAdView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
@interface YLAdView()<SDWebImageManagerDelegate>

@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, weak) UIButton *closeButton;

@end



@implementation YLAdView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        self.backgroundColor = YLColorA(0, 0, 0, 0.4);
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToOther)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
        
        _imageView = imageView;
        [self addSubview:imageView];
        __weak typeof(self) weakSelf = self;
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(weakSelf);
            make.top.mas_equalTo(weakSelf).mas_offset(120);
            make.bottom.mas_equalTo(weakSelf).mas_offset(-120);
            make.width.mas_equalTo(imageView.mas_height).multipliedBy(0.6);
        }];
        
        UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeButton addTarget:self action:@selector(closeSelf) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeButton];
        [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(imageView);
            make.top.mas_equalTo(imageView.mas_bottom).mas_offset(40);
            make.width.height.mas_equalTo(40);
        }];
        [closeButton setBackgroundImage:[UIImage imageNamed:@"closeWhite"] forState:UIControlStateNormal];
        
        UIView *line = [[UIView alloc] init];
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(imageView.mas_bottom);
            make.bottom.mas_equalTo(closeButton.mas_top);
            make.width.mas_equalTo(1);
            make.centerX.mas_equalTo(imageView);
        }];
        line.backgroundColor = [UIColor whiteColor];
        line.alpha = 0.4;
        
        
    }
    return self;
}

- (void)setIconUrl:(NSString *)iconUrl
{
    _iconUrl = iconUrl;
    NSString *url = [NSString stringWithFormat:@"%@%@", YLImageURL, iconUrl];
    __weak typeof(self) weakSelf = self;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        weakSelf.alpha = 1;
    }];
}





- (void)jumpToOther
{
    if (self.clickBlock) {
        self.clickBlock();
        [self removeFromSuperview];
    }
}

- (void)closeSelf
{
    [self removeFromSuperview];
}

@end
